package com.pangyangyang.canal.sample;/*
 * Created by gangzz.
 *  2015/7/3
 */

import com.alibaba.otter.canal.client.impl.SimpleCanalConnector;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.Message;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class SimpleClientMini {

    private String destination = "example";
    private SimpleCanalConnector connector;

    public static void main(String[] args){
        new SimpleClientMini().run();
    }

    public void run(){
        connector = new SimpleCanalConnector(new InetSocketAddress("10.128.170.167",
                11111), "", "", destination);
        try {
            connector.connect();
            connector.subscribe();
            Message message = connector.getWithoutAck(1, 5L, TimeUnit.SECONDS);
            List<CanalEntry.Entry> entryList = message.getEntries();
            if (!entryList.isEmpty()) {
                CanalEntry.Entry entry = entryList.get(0);
                CanalEntry.EntryType entryType = entry.getEntryType();
                System.out.println("entry type " + entryType.name());
            }
        } finally {
            connector.unsubscribe();
            connector.disconnect();
        }
    }
}
